package com.suixinsanren.design.state.study.demo01;

public class GumballMachine {

    private StateEnum state = StateEnum.SOLD_OUT;
    private int count = 0;

    public GumballMachine(int count) {
        this.count = count;
        if (count > 0) {
            state = StateEnum.NO_QUARTER;
        }
    }

    /**
     * 投币
     */
    public void insertQuarter() {
        switch (state) {
            case HAS_QUARTER:
                System.out.println("你不能再插入25美分硬币了");
                break;
            case NO_QUARTER:
                state = StateEnum.HAS_QUARTER;
                System.out.println("你插入了一个25美分的硬币");
                break;
            case SOLD_OUT:
                System.out.println("你不能投25美分硬币，这台机器已经卖完了");
                break;
            case SOLD:
                System.out.println("请稍等，我们已经在给您糖果了");
                break;
            default:
                throw new IllegalArgumentException("非法状态");
        }
    }

    /**
     * 退钱
     */
    public void ejectQuarter() {
        switch (state) {
            case HAS_QUARTER:
                System.out.println("25美分已退回");
                state = StateEnum.NO_QUARTER;
                break;
            case NO_QUARTER:
                System.out.println("你还没有插入25美分的硬币");
                break;
            case SOLD_OUT:
                System.out.println("你不能弹出，你还没有插入25美分硬币");
                break;
            case SOLD:
                System.out.println("抱歉，你已经转动曲柄，不能在退钱");
                break;
            default:
                throw new IllegalArgumentException("非法状态");
        }
    }

    /**
     * 转动曲柄开始游戏
     */
    public void turnCrank() {
        switch (state) {
            case HAS_QUARTER:
                System.out.println("你已经转动曲柄");
                state = StateEnum.SOLD;
                dispense();
                break;
            case NO_QUARTER:
                System.out.println("需要先投入25分钱");
                break;
            case SOLD_OUT:
                System.out.println("你已经转动曲柄，但是已经没有糖果了");
                break;
            case SOLD:
                System.out.println("转两圈不会给你带来另一个糖果");
                break;
            default:
                throw new IllegalArgumentException("非法状态");
        }
    }

    /**
     * 发放糖果
     */
    public void dispense() {
        switch (state) {
            case SOLD:
                System.out.println("一颗糖果从狭缝里滚了出来");
                if (--count == 0) {
                    System.out.println("哎呀，糖果用完了");
                    state = StateEnum.SOLD_OUT;
                } else {
                    state = StateEnum.NO_QUARTER;
                }
                break;
            case NO_QUARTER:
                System.out.println("需要先投入25美分重新开始");
                break;
            case SOLD_OUT:
            case HAS_QUARTER:
                System.out.println("已经没有糖果了");
                break;
            default:
                throw new IllegalArgumentException("非法状态");
        }
    }

    @Override
    public String toString() {
        return "GumballMachine{" +
                "state=" + state +
                ", count=" + count +
                '}';
    }

}
